package com.hangzhou.xyj.ui.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.ActionModel;
import com.hangzhou.xyj.model.BannerModel;
import com.hangzhou.xyj.model.DramaCategoryModel;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.ui.activity.DramaCategoryListActivity;
import com.hangzhou.xyj.ui.activity.WebActivity;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.ui.widget.FamousExpertBannerView;
import com.hangzhou.xyj.ui.widget.views.MyImageView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.ArrayList;
import java.util.List;

/**
 * 发现->演出
 * Created by Administrator on 2016/1/18.
 */
public class FindShowAdapter extends BaseLoadingAdapter<ActionModel> {
    private static final int TYPE_BANNER = 0x11;
    private static final int TYPE_CATEGORY = 0x22;
    private static final int TYPE_NORMAL_SINGLE_IMG = 0x33;
    private static final int TYPE_NORMAL_MUCH_IMG = 0x44;


    private boolean isFirstIn = true;//是否第一次进入
    private List<BannerModel> mBannerList;
    private List<DramaCategoryModel> mDramaList;//剧种List
    private OnDramaChangeListener mOnDramaChangeListener;

    public FindShowAdapter(RecyclerView recyclerView, List<ActionModel> dataList) {
        super(recyclerView, dataList);
    }

    @Override
    public void onBindNormalViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        if (viewHolder == null) return;
        switch (getItemViewType(position)) {
            case TYPE_BANNER:
                BannerViewHolder bannerViewHolder = (BannerViewHolder) viewHolder;
                bannerViewHolder.setData(mBannerList);
                break;
            case TYPE_CATEGORY:
                DramaViewHolder dramaViewHolder = (DramaViewHolder) viewHolder;
                dramaViewHolder.setData(mDramaList);
                break;
            case TYPE_NORMAL_SINGLE_IMG:
                SingleImgViewHolder showViewHolder = (SingleImgViewHolder) viewHolder;
                showViewHolder.setData(mDataList.get(position - 2));
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, WebActivity.class);
                        intent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_FIND_SHOW.getValue());
                        intent.putExtra(DataKey.TITLE, "演出详情");
                        intent.putExtra(DataKey.INDEX, mDataList.get(position - 2).getUrl());
                        intent.putExtra(DataKey.OBJ_ID, mDataList.get(position - 2).getId());
                        intent.putExtra(DataKey.SHARE_TITLE, mDataList.get(position - 2).getName());
                        intent.putExtra(DataKey.SHARE_CONTENT, mDataList.get(position - 2).getIntroduce());
                        intent.putExtra(DataKey.SHARE_URL, mDataList.get(position - 2).getShareUrl());
                        IntentManager.getInstance().goActivity(mContext, intent);
                    }
                });
                break;
            case TYPE_NORMAL_MUCH_IMG:
                MuchImgViewHolder muchImgViewHolder = (MuchImgViewHolder) viewHolder;
                muchImgViewHolder.setData(mDataList.get(position - 2));
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, WebActivity.class);
                        intent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_FIND_SHOW.getValue());
                        intent.putExtra(DataKey.TITLE, "演出详情");
                        intent.putExtra(DataKey.INDEX, mDataList.get(position - 2).getUrl());
                        intent.putExtra(DataKey.OBJ_ID, mDataList.get(position - 2).getId());
                        intent.putExtra(DataKey.SHARE_TITLE, mDataList.get(position - 2).getName());
                        intent.putExtra(DataKey.SHARE_CONTENT, mDataList.get(position - 2).getIntroduce());
                        intent.putExtra(DataKey.SHARE_URL, mDataList.get(position - 2).getShareUrl());
                        IntentManager.getInstance().goActivity(mContext, intent);
                    }
                });
                break;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateNormalViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder;
        switch (viewType) {
            case TYPE_BANNER:
                holder = new BannerViewHolder(mLayoutInflater.inflate(R.layout.layout_banner, parent, false));
                break;
            case TYPE_CATEGORY:
                holder = new DramaViewHolder(mLayoutInflater.inflate(R.layout.layout_custom_grid, parent, false));
                break;
            case TYPE_NORMAL_SINGLE_IMG:
                holder = new SingleImgViewHolder(mLayoutInflater.inflate(R.layout.layout_find_show_item_single_img, parent, false));
                break;
            case TYPE_NORMAL_MUCH_IMG:
            default:
                holder = new MuchImgViewHolder(mLayoutInflater.inflate(R.layout.layout_find_show_item_much_img, parent, false));
                break;
        }
        return holder;
    }

    @Override
    public int getItemCount() {
        if (mDataList == null) {
            return 2;
        }
        setAdditionalSize(2);
        return mDataList.size() + 2;
    }

    @Override
    public int getItemViewType(int position) {
        int imgSize;
        if (position == 0) {
            return TYPE_BANNER;
        } else if (position == 1) {
            return TYPE_CATEGORY;
        } else if (mDataList.get(position - 2) == null) {
            return TYPE_LOADING_ITEM;
        } else {
            imgSize = mDataList.get(position - 2).getImgList().size();
            if (imgSize == 1) {
                return TYPE_NORMAL_SINGLE_IMG;
            } else {
                return TYPE_NORMAL_MUCH_IMG;
            }
        }
    }


    /**
     * 更新Banner数据
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/11 16:55
     */
    public void updateBannerData(List<BannerModel> list) {
        if (list == null || list.size() == 0) {
            list = new ArrayList<>();
        }
        this.mBannerList = list;
        notifyDataSetChanged();
    }

    /**
     * 更新剧种
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/12 16:27
     */
    public void updateDramaCategoryData(List<DramaCategoryModel> list) {
        if (list == null || list.size() == 0) {
            list = new ArrayList<>();
        }
        this.mDramaList = list;
        notifyDataSetChanged();
    }

    public void setOnDramaChangeListener(OnDramaChangeListener listener) {
        this.mOnDramaChangeListener = listener;
    }

    public interface OnDramaChangeListener {
        void OnDramaChange(int catId, boolean isReload);
    }

    /**
     * 图片轮播的ViewHolder
     */
    private class BannerViewHolder extends RecyclerView.ViewHolder {
        private FamousExpertBannerView mItemView;

        public BannerViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        public void initView(View view) {
            mItemView = (FamousExpertBannerView) view.findViewById(R.id.view_famous_expert_banner);
        }

        public void setData(List<BannerModel> list) {
            mItemView.setData(list);
        }
    }

    /**
     * 剧种的ViewHolder
     */
    private class DramaViewHolder extends RecyclerView.ViewHolder {
        private GridView mGridView;
        private DramaAdapter mAdapter;

        public DramaViewHolder(View itemView) {
            super(itemView);
            mGridView = (GridView) itemView.findViewById(R.id.grid_view);
            mGridView.setNumColumns(5);
            mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> adapter, View view,
                                        int position, long id) {
                    //如果点击的是更多则跳转
                    if (position == 9) {
                        Intent intent = new Intent(mContext, DramaCategoryListActivity.class);
                        intent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_FIND_SHOW.getValue());
                        IntentManager.getInstance().goActivity(mContext, intent);
                    } else {
                        //刷新选中颜色,当前为选中色，其他为未选中
                        for (int i = 0; i < mDramaList.size(); i++) {
                            if (i == position) {
                                mDramaList.get(i).setIsSelected(true);
                            } else {
                                mDramaList.get(i).setIsSelected(false);
                            }
                        }
                        mOnDramaChangeListener.OnDramaChange(mDramaList.get(position).getId(), false);
                    }
                }
            });
        }

        public void setData(List<DramaCategoryModel> list) {
            //第一次进入的时候 第一个选中
            if (isFirstIn && list != null && list.size() > 0) {
                list.get(0).setIsSelected(true);
                isFirstIn = false;
            }
            mGridView.setAdapter(mAdapter = new DramaAdapter(mContext, list));
        }

    }

    private class SingleImgViewHolder extends RecyclerView.ViewHolder {
        MyTextView name;//标题
        MyTextView content;//介绍
        MyTextView seat;//座位数
        MyTextView time;//时间
        MyTextView location;//地址
        MyImageView pic;//图片

        public SingleImgViewHolder(View view) {
            super(view);
            name = (MyTextView) view.findViewById(R.id.mtv_title);
            content = (MyTextView) view.findViewById(R.id.mtv_content);
            seat = (MyTextView) view.findViewById(R.id.mtv_seat);
            time = (MyTextView) view.findViewById(R.id.mtv_time);
            location = (MyTextView) view.findViewById(R.id.mtv_location);
            pic = (MyImageView) view.findViewById(R.id.miv_image);
        }

        public void setData(ActionModel actionModel) {
            name.setText(actionModel.getName());
            content.setText(actionModel.getIntroduce());
            seat.setText("" + actionModel.getPartNum());
            time.setText(actionModel.getTime());
            location.setText(actionModel.getLocation());
            pic.displayImage(actionModel.getImgList().get(0));
        }
    }

    private class MuchImgViewHolder extends RecyclerView.ViewHolder {
        MyTextView name;//标题
        MyTextView content;//介绍
        MyTextView seat;//座位数
        MyTextView time;//时间
        MyTextView location;//地址
        MyImageView pic1;//图片1
        MyImageView pic2;//图片2
        MyImageView pic3;//图片3

        public MuchImgViewHolder(View view) {
            super(view);
            name = (MyTextView) view.findViewById(R.id.mtv_title);
            content = (MyTextView) view.findViewById(R.id.mtv_introduce);
            seat = (MyTextView) view.findViewById(R.id.mtv_seat);
            time = (MyTextView) view.findViewById(R.id.mtv_time);
            location = (MyTextView) view.findViewById(R.id.mtv_location);
            pic1 = (MyImageView) view.findViewById(R.id.miv_image1);
            pic2 = (MyImageView) view.findViewById(R.id.miv_image2);
            pic3 = (MyImageView) view.findViewById(R.id.miv_image3);
        }

        public void setData(ActionModel actionModel) {
            name.setText(actionModel.getName());
            content.setText(actionModel.getIntroduce());
            seat.setText("" + actionModel.getPartNum());
            time.setText(actionModel.getTime());
            location.setText(actionModel.getLocation());
            pic1.displayImage(actionModel.getImgList().get(0));
            pic2.displayImage(actionModel.getImgList().get(1));
            if (actionModel.getImgList().size() == 3) {
                pic3.displayImage(actionModel.getImgList().get(2));
            } else {
                pic3.setVisibility(View.GONE);
            }
        }
    }


}
